package com.ITArmy.controller.otherLogin;

import com.ITArmy.beans.Blog;
import com.ITArmy.beans.Shows.ReadBlog;
import com.ITArmy.beans.User;
import com.ITArmy.common.ServerResponse;
import com.ITArmy.dao.CommentMapper;
import com.ITArmy.dao.PraiseMapper;
import com.ITArmy.dao.UserMapper;
import com.ITArmy.service.BlogService;
import com.ITArmy.service.OtherLoginService;
import com.ITArmy.service.TypeService;
import com.ITArmy.util.login.GitHubConstants;
import com.ITArmy.util.login.HttpClientUtils;
import com.alipay.api.internal.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
public class GitHubCallBackController {
    @Autowired
    OtherLoginService otherLoginService;
    @Autowired
    TypeService typeService;
    @Autowired
    BlogService blogService;
    @Autowired
    UserMapper userMapper;
    @Autowired
    PraiseMapper praiseMapper;
    @Autowired
    CommentMapper commentMapper;

    /**
     * 授权成功后的回调,我们需要在这个方法中拿到code去请求token
     */
    @RequestMapping("/callback")
    public String callback(String code, String state, HttpServletRequest request, Model model) throws Exception {
        //获取到code和state
        System.out.println("code:" + code);
        System.out.println("state:" + state);

        if (!StringUtils.isEmpty(code) && !StringUtils.isEmpty(state)) {
            //拿到我们的code,去请求token
            //发送一个请求到
            String token_url = GitHubConstants.TOKEN_URL.replace("CLIENT_ID", GitHubConstants.CLIENT_ID)
                    .replace("CLIENT_SECRET", GitHubConstants.CLIENT_SECRET)
                    .replace("CALLBACK", GitHubConstants.CALLBACK)
                    .replace("CODE", code);
            //   System.out.println("用户信息数据"+token_url);//这个里面有我们想要的用户信息数据
            String responseStr = HttpClientUtils.doGet(token_url);
            String token = HttpClientUtils.parseResponseEntity(responseStr).get("access_token");
            //根据token发送请求获取登录人的信息
            String userinfo_url = GitHubConstants.USER_INFO_URL.replace("TOKEN", token);
            responseStr = HttpClientUtils.doGet(userinfo_url);
            //json
            //获取到的全部的用户信息
            Map<String, String> responseMap = HttpClientUtils.parseResponseEntityJSON(responseStr);
            User user = new User();
            user.setuHead(responseMap.get("avatar_url"));
            user.setuName(responseMap.get("login"));
            user.setuTime(new Date());
            user.setuId(Long.parseLong(responseMap.get("id")));
            user.setCoId(1);
            ServerResponse serverResponse = otherLoginService.gitLogin(request, user);
            if (serverResponse.isSuccess()) {
                ServerResponse typeServerResponse = typeService.typeList();
                model.addAttribute("allBlogType", typeServerResponse.getData());
                //获取推荐的博客
                List<Blog> blogs = (List) blogService.recommendList().getData();

                List<ReadBlog> recommendBlog = new ArrayList();
                for (int i = blogs.size() - 1; i >= 0; i--) {
                    ReadBlog readBlog = new ReadBlog();
                    //博客信息
                    readBlog.setBlog(blogs.get(i));
                    //用户信息
                    readBlog.setUser(userMapper.selectByPrimaryKey(blogs.get(i).getuId()));
                    //点赞数量
                    readBlog.setLikeNumber(praiseMapper.selectBlogPraiseCount(blogs.get(i).getbId()).size());
                    //评论数量
                    readBlog.setCommentNumber(commentMapper.findCommentByBlogId(blogs.get(i).getuId()).size());
                    recommendBlog.add(readBlog);
                }
                model.addAttribute("recommendBlog", recommendBlog);

                return "redirect:/user/index";
            }
            return "sign";
        }
        return "sign";
        // TODO 修改成自己需要返回的页面...
    }
}

